<script setup lang="ts">
import { ref, computed, onMounted } from "vue";
import { useStore } from "vuex";
import {useRouter} from "vue-router"
import { addTalk, getTalkList } from "@/api/talk.js";
import TextArea from "@/components/textArea.vue";
import { beforeTime } from "@/utils/timeInterval.js";
import {organizeComments} from "@/utils/formatMessageList.js";
const $store = useStore();
const router = useRouter();
let showMessage = ref(false);
const textAreaRef = ref();
const replyList = ref([]);
let showEmoji = ref(false);
const $props = defineProps(["newsItem"]);
onMounted(() => {
  getReplyList();
});

const getReplyList = async () => {
  const res = await getTalkList({ forumId: $props.newsItem.forumId });
  console.log(res, "response");
  let arr = organizeComments(res.data);
  console.log(arr, "处理后的数组");
  replyList.value = arr;
};

const handleChangeEmoji = () => {
  console.log(11);
  showEmoji.value = !showEmoji.value;
};

const handleChangeMessage = () => {
  if(!$store.state.user.token) {
    router.push({path:"/login"});  return;
  }
  showMessage.value = !showMessage.value;
};

const handleInputChange = (e) => {
  let value = e.target.innerHTML;
  if (value) {
    e.target.classList.add("has-content");
  } else {
    e.target.classList.remove("has-content");
  }
};

const handleSelectEmoji = (url) => {
  console.log(url, "--url--");
  handleChangeEmoji();
  createImg(url);
};

const createImg = (url) => {
  const img = document.createElement("img");
  img.src = url;
  textAreaRef.value.appendChild(img);
  // 创建一个新的 Range 对象
  const range = new Range();
  // 将 Range 对象设置为 textAreaRef 的末尾
  range.selectNodeContents(textAreaRef.value);
  range.collapse(false); //是否全部选中
  // 获取当前的 Selection 对象
  const selection = window.getSelection();
  // 清除之前的选区
  selection.removeAllRanges();
  // 将新的 Range 对象添加到选区中
  selection.addRange(range);
  if (textAreaRef.value.innerHTML !== "") {
    textAreaRef.value.classList.add("has-content");
  } else {
    textAreaRef.value.classList.remove("has-content");
  }
};

const handleOnSubmit = async () => {
  const requestObj = {};
  let content = textAreaRef.value.innerHTML;
  let userId = $store.state.user.userInfo.id;
  const { forumId, groupId, id } = $props.newsItem;
  if (content) {
    requestObj.content = content;
  }
  if (userId) {
    requestObj.userId = userId;
  }
  if (forumId) {
    requestObj.forumId = forumId;
  }
  if (groupId) {
    requestObj.groupId = groupId;
  }
  requestObj.istop = 0;

  console.log(requestObj, "@@");
  await addTalk(requestObj);
  getReplyList();
  textAreaRef.value.innerHTML = "";
};

const TextareaItemClose = (item) => {
  console.log(item, "2@@");
  item.show = false;
};

const handleSubmitReply = async (requestObj, item) => {
  await addTalk(requestObj);
  getReplyList();
  TextareaItemClose(item);
};

// 把枚举code转换为text
const handleCodeToText = (code) => {
  console.log(code, "枚举");
  let tmp = TalkEnumList.value.find((item) => item.enumCode === code);
  return tmp.enumText;
};

// 计算属性
const avatar = computed(() => {
  return $store.state.user.userInfo.avatar;
});
const TalkEnumList = computed(() => {
  return $store.state.talkEnumTypeList;
});
</script>

<template>
  <el-card class="news-item">
    <!-- 用户信息 -->
    <div class="news-user">
      <div class="pic">
        <img :src="newsItem.userInfo.avatar" alt="" />
      </div>
      <div class="name">{{ newsItem.userInfo.nickname }}</div>
      <span class="time">{{ beforeTime(newsItem.createTime) }}</span>
    </div>
    <!-- 评论内容 -->
    <div class="news-content" v-html="newsItem.content"></div>
    <el-tag v-if="newsItem.groupId">{{
      handleCodeToText(newsItem?.groupId)
    }}</el-tag>
    <div class="news-pic" v-if="newsItem.imgUrl">
      <div
        class="pic-box"
        v-for="imgItem in newsItem.imgUrl.split(',')"
        :key="imgItem"
      >
        <img :src="imgItem" alt="" />
      </div>
    </div>
    <div class="news-link" v-if="newsItem.link">
      <div
        v-for="item in newsItem.link.split(',')"
        :key="item"
        class="link-item"
      >
        <div class="left">
          <el-icon><Link style="width: 1.5em; height: 1.5em" /></el-icon>
        </div>
        <div class="info">
          <p>{{ item }}</p>
          <p>{{ item }}</p>
        </div>
      </div>
    </div>
    <div class="news-footer">
      <div class="comment" @click="handleChangeMessage">
        <el-icon><ChatDotRound /></el-icon>
      </div>
      <div class="zan">
        <el-icon><ChatDotRound /></el-icon>
      </div>
    </div>
    <div class="comment" v-if="showMessage">
      <h4>评论</h4>
      <!-- 评论窗口 -->
      <div class="comment-box">
        <div class="comment-avatar">
          <img :src="avatar" alt="" />
        </div>
        <div class="comment-right">
          <div
            class="comment-textarea"
            contenteditable="true"
            ref="textAreaRef"
            @input="handleInputChange"
            placeholder="说点什么呢"
          ></div>
          <div class="bottom-btn">
            <svg-icon name="emoil" @click="handleChangeEmoji"></svg-icon>
            <el-button type="danger" @click="handleOnSubmit">发表</el-button>
          </div>
          <div class="emoji-wrap" v-if="showEmoji">
            <div class="emoji">
              <Emoji @changeEmoji="handleSelectEmoji" />
            </div>
          </div>
        </div>
      </div>

      <div class="reply-news">
        <div
          class="news-item"
          v-for="item in replyList"
          :key="item.id"
          :class="{ replyClass: item.replyId ? true : false }"
          @mouseenter="() => (item.hover = true)"
          @mouseleave="() => (item.hover = false)"
        >
          <div class="news-user">
            <div class="left">
              <div class="pic">
                <img :src="item.userInfo.avatar" alt="" />
              </div>
              <div class="info">
                <div class="name">{{ item.userInfo.nickname }}</div>
                <div class="time">发布{{ beforeTime(item.createTime) }}</div>
              </div>
            </div>
            <div class="btn" :class="{ hover: item.hover ? true : false }">
              <el-button
                type="danger"
                size="small"
                @click="() => (item.show = true)"
                >回复</el-button
              >
            </div>
          </div>
          <div class="content" v-html="item.content"></div>
          <TextArea
            v-if="item.show"
            @close="TextareaItemClose(item)"
            :item="item"
            @submit="handleSubmitReply"
          />
        </div>
      </div>
    </div>
  </el-card>
</template>

<style scoped lang="scss">
:deep .el-card__body {
  padding-bottom: 0;
}
.news-item {
  margin-bottom: 20px;
  .news-pic {
    display: flex;
    .pic-box {
      width: 150px;
      margin-right: 10px;
      img {
        border-radius: 5px;
        width: 100%;
      }
    }
  }

  .news-link {
    .link-item {
      margin-top: 10px;
      position: relative;
      width: 100%;
      display: flex;
      padding: 10px;
      border:1px solid #e4e7ed;
      background-color: #fff;
      .left {
        margin-right: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        background-color: rgb(0, 115, 255);
        svg {
          color: #fff;
        }
        :deep .el-icon {
          font-size: 20px;
        }
      }
      .info {
        p {
          font-size: 14px;
        }
        p:first-child {
          font-size: 16px;
          font-weight: 600;
        }
      }
    }
  }
  .news-user {
    display: flex;
    align-items: center;

    .pic {
      margin-right: 10px;
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .name {
      margin-right: 10px;
      color: $strong;
    }
    .time {
      font-size: 12px;
      color: #c5bdbd;
      margin-right: 10px;
    }
  }
  .news-content {
    display: flex;
    align-items: center;
    margin-top: 20px;
    margin-bottom: 10px;
  }
  .news-footer {
    display: flex;
    margin-top: 20px;
    border-top: 1px solid #e4e7ed;
    .comment {
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 10px;
      flex: 1;
      text-align: center;
    }
    .zan {
      cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 10px;
      flex: 1;
    }
  }
  .comment {
    .comment-box {
      padding: 10px;
      margin-top: 10px;
      border: 1px solid red;
      display: flex;
      justify-content: space-between;
      .comment-avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .comment-right {
        flex: 1;
        position: relative;
        .comment-textarea {
          min-height: 122px;
          position: relative;
          padding-top: 10px;
          padding-left: 10px;
          margin-left: 10px;
          // border: 1px solid cyan;
          &::after {
            font-size: 14px;
            content: attr(placeholder);
            color: #666;
            position: absolute;
            top: 10px;
          }
          &.has-content::after {
            display: none;
          }
        }
        .bottom-btn {
          align-items: center;
          margin-top: 10px;
          padding: 0 10px;
          display: flex;
          justify-content: space-between;
        }
        :deep svg {
          cursor: pointer;
        }
        .emoji-wrap {
          position: absolute;
          left: 20px;
          top: -70px;
          z-index: 10;
        }
      }
    }
    .reply-news {
      margin-top: 10px;
      .news-item {
        margin-bottom: 10px;
        &.replyClass {
          padding-left: 20px;
        }
        .btn {
          opacity: 0;
          transition: all 0.2s ease;
          &.hover {
            opacity: 1;
            // display: block;
          }
        }
        .news-user {
          display: flex;
          justify-content: space-between;
          align-items: flex-end;
          margin-bottom: 10px;
          .left {
            display: flex;
          }
        }
        .content {
          display: flex;
          align-items: center;
          background: #f5f5f5;
          padding: 10px;
        }
      }
    }
  }
}
</style>
